package com.zfei.gfb.controller;

import cn.hutool.core.date.DateUtil;
import com.google.common.collect.Lists;
import com.zfei.gfb.bean.Const;
import com.zfei.gfb.bean.Status;
import com.zfei.gfb.bean.StatusException;
import com.zfei.gfb.core.Auth;
import com.zfei.gfb.core.HttpJson;
import com.zfei.gfb.core.LogResult;
import com.zfei.gfb.dao.GfbNoticeMapper;
import com.zfei.gfb.device.AppContext;
import com.zfei.gfb.model.GfbNotice;
import com.zfei.gfb.model.GfbUser;
import com.zfei.gfb.query.GfbNoticeQuery;
import com.zfei.gfb.request.GfbDelRequest;
import com.zfei.gfb.request.GfbNoticeAddRequest;
import com.zfei.gfb.request.GfbNoticeEditRequest;
import com.zfei.gfb.request.GfbSetRequest;
import com.zfei.gfb.util.Beans;
import com.zfei.gfb.util.Now;
import com.zfei.gfb.vo.GfbNoticeListVo;
import com.zfei.gfb.vo.GfbNoticeVo;
import com.zfei.gfb.vo.GfbUserListVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @author
 * @version 1.0
 */
@Auth(type = {Const.VILLAGE_MANAGER, Const.TOWN_MANAGER})
@Api(tags = "通知公告接口")
@RestController
@RequestMapping("/web/notice")
public class GfbNoticeController {

    @Resource
    private GfbNoticeMapper gfbNoticeMapper;

    @Resource
    private HttpSession httpSession;

    @ApiOperation(value = "列表")
    @ApiResponses({
            @ApiResponse(code = 200, message = "",response = GfbNoticeListVo.class),
    })
    @LogResult
    @HttpJson("/list")
    public Object list(@RequestBody GfbNoticeQuery query) {
        query.build(Now::millis);

        GfbNoticeListVo result = new GfbNoticeListVo();

        int total = gfbNoticeMapper.total(query);
        result.setTotal(total);
        if (total == 0) {
            result.setData(Lists.newArrayList());
            return result;
        }
        List<GfbNotice> records = gfbNoticeMapper.list(query);
        List<GfbNoticeVo> vos = Beans.copys(records, GfbNoticeVo.class);
        result.setData(vos);
        return result;

    }

    @ApiOperation(value = "新增")
    @LogResult
    @HttpJson("/add")
    public Object add(@RequestBody GfbNoticeAddRequest request) {
        //默认下线
        request.checkInsertNull();
        GfbNotice record = Beans.copy(request, GfbNotice.class);

        GfbUser user = (GfbUser) httpSession.getAttribute(AppContext.SESSION_WEB_USER);
        record.setStatus(Const.STATUS_OFF);
        record.setCreateTime(Now.millis());
        record.setUpdateTime(Now.millis());
        record.setCntime(DateUtil.now());
        record.setUserId(user.getId());
        record.setPubTime(Now.millis());

        gfbNoticeMapper.insertSelective(record);
        return null;
    }

    @ApiOperation(value = "编辑")
    @LogResult
    @HttpJson("/edit")
    public Object edit(@RequestBody GfbNoticeEditRequest request) {

        request.checkUpdateNull();

        GfbNotice record = gfbNoticeMapper.selectByPrimaryKey(request.getId());
        if (record == null) {
            throw new StatusException(Status.NOT_EXIST);
        }

        BeanUtils.copyProperties(request, record);
        record.setUpdateTime(Now.millis());
        record.setPubTime(Now.millis());

        gfbNoticeMapper.updateByPrimaryKeySelective(record);

        return null;
    }

    @ApiOperation(value = "删除")
    @LogResult
    @HttpJson("/del")
    public Object del(@RequestBody GfbDelRequest request) {
        request.checkDelNull();
        Integer id = request.getId();

        GfbNotice record = gfbNoticeMapper.selectByPrimaryKey(id);
        if (record == null) {
            throw new StatusException(Status.NOT_EXIST);
        }

        gfbNoticeMapper.deleteByPrimaryKey(id);
        return null;
    }

    @ApiOperation(value = "状态")
    @LogResult
    @HttpJson("/set")
    public Object set(@RequestBody GfbSetRequest request) {
        request.checkSetNull(Const::legalTwoStatus);

        Integer id = request.getId();
        Integer status = request.getStatus();

        GfbNotice record = gfbNoticeMapper.selectByPrimaryKey(id);
        if (record == null) {
            throw new StatusException(Status.NOT_EXIST);
        }
        record.setStatus(status);
        record.setUpdateTime(Now.millis());
        gfbNoticeMapper.updateByPrimaryKeySelective(record);

        return null;
    }

}
